<template>
  <div>
      <common-head />
      <div class="wrap">
        <div class="logo">
          <img src="https://m.800pharm.com/images/wap/log_img.png" alt="">
        </div>
    <van-form @submit="onSubmit">
  <van-field
    v-model="userInfo.username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="userInfo.pwd"
    type="password"
    name="pwd"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
  <van-button @click="$router.push('/register')">去注册</van-button>
</div>
  </div>
</template>

<script>
import CommonHead from 'components/CommonHead'
import { doLogin } from 'api'
import { Toast } from 'vant'
export default {
  data () {
    return {
      userInfo: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (params) {
      params = {
        ...params,
        deviceId: 1111,
        deviceName: 'dhwidw'
      }
      doLogin(params).then(res => {
        if (res.data.code === 0) {
          localStorage.setItem('token', res.data.data.token)
          Toast.success({
            message: '登录成功',
            duration: 1000,
            onClose: () => {
              // 跳转到其他页面
              if (this.$route.query.from) {
                this.$router.replace(this.$route.query.from)
              } else {
                this.$router.replace('/home')
              }
            }
          })
        } else {
          Toast.fail('登录失败')
        }
      })
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  .logo{
    text-align: center;
  } img{
    width: 187px;
    height: 120px;
  }
}
</style>
